﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Scheduling.Models.Repos.Database.Objects;
using System.Web.UI.DataVisualization.Charting;
using Scheduling.Models;
using Scheduling.Models.Interfaces;
using System.Data;
using System.IO;
using System.ComponentModel;
using Scheduling.Models.Helpers;

namespace Scheduling.Controllers
{
    public class StatisticsController : Controller
    {
        public ActionResult Bookings()
        {
            return View();

        }

        public ActionResult GenerateChart(Scheduling.Models.Helpers.StatisticsHelper.ChartType Type)
        {

            System.Web.Helpers.Chart chart = new System.Web.Helpers.Chart(500, 300);
            var bookingLoader = IoC.Resolve<IBookingLoader>();

            var bookingCount = new List<int>();
            switch (Type)
            {
                case Scheduling.Models.Helpers.StatisticsHelper.ChartType.TopUsers:
                    var userList = StatisticsHelper.GetTopUsers();

                    var userIDs = new List<string>();

                    foreach (var user in userList)
                    {
                        userIDs.Add(user.ID);
                        bookingCount.Add(bookingLoader.GetBookingsByUserID(user.ID).Count);
                    }

                    chart.AddSeries("s", "Column", null, null, null, 1, userIDs, "user", bookingCount, "bookings");
                    
                    break;
                case Scheduling.Models.Helpers.StatisticsHelper.ChartType.TopRooms:

                    var roomList = StatisticsHelper.GetTopRooms();

                    var roomNames = new List<string>();

                    foreach (var room in roomList)
                    {
                        roomNames.Add(room.ID);
                        bookingCount.Add(StatisticsHelper.GetNumBookingsForRoom(room));
                    }

                    chart.AddSeries("s", "Column", null, null, null, 1, roomNames, "room", bookingCount, "bookings");
                    break;

                case Scheduling.Models.Helpers.StatisticsHelper.ChartType.TopDays:

                    var bookingList = bookingLoader.GetBookings();
                    var bookingsPerDay = StatisticsHelper.GetBookingsPerDay(bookingList);
                    var days = StatisticsHelper.GenerateDayList();

                    chart.AddSeries("s", "Column", null, null, null, 1, days, "day", bookingsPerDay, "bookings");
                    break;
            }
            byte[] bytes = chart.GetBytes("png");
            return File(bytes, "image/png");
        }


    }
}
